/// @FUNCTION
/// @NAME XfHashSet
/// @DESC Set a value into an hash array.
/// @ACCESS Server
/// @MP False
/// @PARAM Array The hash table
/// @PARAM String The name of the key in the hash you want to set
/// @PARAM Variant The value that you want to set in the hash
/// @RETURN Boolean Succeeded or not
/// @REQUIRE Nothing
/// @REMARK
/// @EXAMPLE _success = [_instances, "Reinforcements", [123,234]] call CFUNC(HashSet);

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "arrays\fn_hashSet.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>
#ifndef __DUMP_INDENT__
#define __DUMP_INDENT__	"    "
#endif

private ["_hash_array","_hash_key","_hash_indexer","_index","_hash_values","_result","_value"];
TRACE_1("Start",_this);
TMFPROFILERSTART;
_result = false;
if (typeName _this != typename []) exitWith { ERROR_1("Wrong Input",_this); };
if (count _this < 3) exitWith { ERROR_1("Wrong Input",_this); };
_hash_array = _this select 0;
_hash_key = _this select 1;
_value = _this select 2;
_result = _hash_array;
if ((typeName _hash_array != typename []) || (typeName _hash_key != typename "")) exitWith { ERROR_1("Wrong Input",_this); };
// For safety and easyness, we Upper all keys internally
_hash_key = toUpper _hash_key;
// Normally "== 2" is enough since a hash only needs a key indexer and a value array, but in the future we might extend it to more keys for multi dimensional storage
//  So they 1st dimension keys array is always the first and the values container is always the last one
if (count _hash_array <= 0) then { _hash_array set [0, []];};				
if (count _hash_array <= 1) then { _hash_array set [1, []];};				
_hash_indexer = _hash_array select 0;
_hash_values = _hash_array select (count _hash_array - 1);
If !(_hash_key in _hash_indexer) then {
	_hash_indexer set [count _hash_indexer, _hash_key];
};
_index = _hash_indexer find _hash_key;
_hash_values set[_index, _value];
_hash_array set [0, _hash_indexer];
_hash_array set [(count _hash_array - 1),_hash_values];
_result = _hash_array;
TRACE_1("End",_result);
TMFPROFILERSTOP;
_result;